﻿var app = angular.module("app", ['ngRoute']);
app.config(function ($routeProvider) {
    $routeProvider.when("/", {
        templateUrl: "Views/home.html",
        controller: "mainController"
    }).when("/Category/:category", {
        templateUrl: "Views/home.html",
        controller: "mainController"
    }).when("/Add", {
        templateUrl: "Views/addBeverage.html",
        controller: "addBeverageController"
    }).when("/Search", {
        templateUrl: "Views/list.html",
        controller: "listController"
    }).when("/:id",
    {
        templateUrl: "Views/home.html",
        controller: "mainController"
    })
        .otherwise({
            templateUrl: "Views/error.html"
        });
});
app.factory("Beverages", function () {
    return [];
});
app.factory("Categories", function () {
    return [{ name: "Alcohol", link: "Alcohol" },
            { name: "Tea & Coffee", link: "TeaCoffee" },
            { name: "Misc", link: "Other" }];
});
app.filter("reverse", function () {
    return function (item) {
        return item.split("").reverse().join("");
    };
});
app.controller("navController",
    function ($scope, Beverages, Categories) {
        $scope.navs = Categories;
    });
//Rendered in view
app.controller("mainController",
    function ($scope, $http, Beverages, $routeParams) {
        $scope.beverages = Beverages;
        $scope.category = "All Beverages";
        if ($routeParams.category) {
            $scope.category = $routeParams.category;
        }
        if ($routeParams.id) {
            $scope.currentIndex = $routeParams.id;
        }
        else {
        $scope.currentIndex = 0;
        }
        $scope.params = $routeParams;
        $scope.currentBeverage = Beverages[$scope.currentIndex];
        $scope.next = function () {
            $scope.currentIndex++;
            if ($scope.currentIndex > Beverages.length - 1) {
                $scope.currentIndex = 0;
            }
            $scope.currentBeverage = Beverages[$scope.currentIndex];
        };
        $scope.prev = function () {
            $scope.currentIndex--;
            if ($scope.currentIndex < 0) {
                $scope.currentIndex = Beverages.length - 1;
            }

            $scope.currentBeverage = Beverages[$scope.currentIndex];
        };
        $scope.notify = function () { };
        $http.get("https://pokedexalex.firebaseio.com/.json"
            ).success(
            function (data) {
                while (Beverages.length) {
                    Beverages.pop();
                }
                for (var x in data) {
                    data[x].key = x;
                    Beverages.push(data[x]);
                }
                $scope.currentBeverage = Beverages[$scope.currentIndex];
            }
            ).error(
            function () { console.log("Error on Get"); }
            );
    });
app.controller("addBeverageController",
    function ($scope, $http, Beverages, Categories, $location) {
        $scope.categories = Categories;
        $scope.saveBeverage = function (beverage) {
            $http
            .post("https://pokedexalex.firebaseio.com/.json", beverage)
            .success(
            function (data) {
                beverage.key = data.name;
                var location = (Beverages.push(beverage))-1;
                $location.path("/"+location);
            })
            .error(
            function () {
                console.log("Error");
            });

        };
    });
app.controller("editBeverageController",
    function ($scope, $http, Beverages) {

    });
app.controller("listController",
    function (Beverages, $scope, $http) {
        $scope.beverages = Beverages;
    });
